<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: liguohua@clcw.com.cn  Blog:http://blog.sina.com.cn/saverneo        |
// +------------------------------------------------------------------------+
//


namespace AuctionCore\Common;
class AjaxPage {
    // 分页栏每页显示的页数
    public $rollPage = 5;
    // 页数跳转时要带的参数
    public $parameter  ;
    // 默认列表每页显示行数
    public $listRows = 20;
    // 起始行数
    public $firstRow ;
    // 分页总页面数
    protected $totalPages  ;
    // 总行数
    protected $totalRows  ;
    // 当前页数
    protected $nowPage    ;
    // 分页的栏的总页数
    protected $coolPages   ;
    // 分页显示定制
    protected $config  = array(
        'header'=>'条记录',
        'prev'=>'上一页',
        'next'=>'下一页',
        'first'=>'第一页',
        'last'=>'最后一页',
        'theme'=>' %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first%  %prePage%  %linkPage%  %nextPage% %end% %jumpform%');
    // 默认分页变量名
    protected $varPage;
    // 同一页显示多个分页时 需要区分 跳转输入框
    public  $jumpInputClassName = 'jumpInputClassTag1';
    public function __construct($totalRows,$listRows='',$ajax_func,$parameter='') {
        $this->totalRows = $totalRows;
        $this->ajax_func = $ajax_func;
        $this->parameter = $parameter;
        $this->varPage = C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ;
        if(!empty($listRows)) {
            $this->listRows = intval($listRows);
        }
        $this->totalPages = ceil($this->totalRows/$this->listRows);     //总页数
        $this->coolPages  = ceil($this->totalPages/$this->rollPage);
        $this->nowPage  = !empty($_GET[$this->varPage])?intval($_GET[$this->varPage]):1;
        if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
            $this->nowPage = $this->totalPages;
        }
        $this->firstRow = $this->listRows*($this->nowPage-1);
    }
    public function setConfig($name,$value) {
        if(isset($this->config[$name])) {
            $this->config[$name]    =   $value;
        }
    }

    /*拍卖站用分页*/
    public function show() {
        if(0 == $this->totalRows) return '';
        $p = $this->varPage;
        $nowCoolPage      = ceil($this->nowPage/$this->rollPage);
        $url  =  $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;
        $parse = parse_url($url);
        if(isset($parse['query'])) {
            parse_str($parse['query'],$params);
            unset($params[$p]);
            $url   =  $parse['path'].'?'.http_build_query($params);
        }
        //上下翻页字符串
        $upRow   = $this->nowPage-1;
        $downRow = $this->nowPage+1;

        if ($upRow>0){
            $upPage = '<span name="prev" class="disabled" style="cursor: pointer;" onclick="javascript:'.$this->ajax_func.'('.$upRow.');">'.$this->config['prev'].'</span>';
        }else{
            $upPage="";
        }
        if ($downRow <= $this->totalPages){
            $downPage='<span name="next" class="pageNext" style="cursor: pointer;" onclick="javascript:'.$this->ajax_func.'('.$downRow.');">'.$this->config['next'].'</span>';
        }else{
            $downPage="";
        }
        // << < > >>
        if($nowCoolPage == 1){
            $theFirst = "";
            $prePage = "";
        }else{
            $preRow =  $this->nowPage-$this->rollPage;
            $theFirst = '<span name="first" class="disabled" style="cursor: pointer;" onclick="javascript:'.$this->ajax_func.'(1);">'.$this->config['first'].'</span>';
            $prePage = '<span name="next" class="pageNext" style="cursor: pointer;" onclick="javascript:'.$this->ajax_func.'('.$preRow.');" title="上'.$this->rollPage.'页">…</span>';
        }
        if($nowCoolPage == $this->coolPages){
            $nextPage = "";
            $theEnd="";
        }else{
            $nextRow = $this->nowPage+$this->rollPage;
            $theEndRow = $this->totalPages;
            $nextPage = '<span name="next" class="pageNext" style="cursor: pointer;" onclick="javascript:'.$this->ajax_func.'('.$nextRow.');" title="下'.$this->rollPage.'页">…</span>';
            $theEnd = '<span name="last" class="pageLast" style="cursor: pointer;"  onclick="javascript:'.$this->ajax_func.'('.$theEndRow.');">'.$this->config['last'].'</span>';


        }
        // 1 2 3 4 5
        $linkPage = "";
        for($i=1;$i<=$this->rollPage;$i++){
            $page=($nowCoolPage-1)*$this->rollPage+$i;
            if($page!=$this->nowPage){
                if($page<=$this->totalPages){
                    $linkPage .= '<span name="nav" style="cursor: pointer;" onclick="javascript:'.$this->ajax_func.'('.$page.');"><a class="navi"><em>'.$page.'</em></a></span>';
                }else{
                    break;
                }
            }else{
                if($this->totalPages != 1){
                    $linkPage .= '<span name="nav" style="cursor: pointer;"><a class="navi"><em><font style="color:#fff">'.$page.'</font></em></a></span>';


                }
            }
        }
        //页面跳转框
        if ($this->totalPages>1){
            $jumpform = '<span>到第</span><input class="cssTxt '.$this->jumpInputClassName.'" type="text" size="1"><span>页</span> <span name="go" onclick="javascript:'.$this->ajax_func.'($(\'.'.$this->jumpInputClassName.'\').val());" class="pageGo">确认</span>';
        }else{
            $jumpform = '';
        }
        $pageStr  =  str_replace(
            array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%linkPage%','%nextPage%','%end%','%jumpform%'),
            array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$downPage,$theFirst,$prePage,$linkPage,$nextPage,$theEnd,$jumpform),$this->config['theme']);
        return $pageStr;
    }
    /*
     * 未用
     * */
    public function _show()
    {
        if (0 == $this->totalRows) return '';
        $p = $this->varPage;
        $nowCoolPage = ceil($this->nowPage / $this->rollPage);
        $url = $_SERVER['REQUEST_URI'] . (strpos($_SERVER['REQUEST_URI'], '?') ? '' : "?") . $this->parameter;
        $parse = parse_url($url);
        if (isset($parse['query'])) {
            parse_str($parse['query'], $params);
            unset($params[$p]);
            $url = $parse['path'] . '?' . http_build_query($params);
        }
        //上下翻页字符串
        $upRow = $this->nowPage - 1;
        $downRow = $this->nowPage + 1;
        if ($upRow > 0) {
            $upPage = "<a id='big' href='javascript:" . $this->ajax_func . "(" . $upRow . ")'>" . $this->config['prev'] . "</a>";
        } else {
            $upPage = "";
        }
        if ($downRow <= $this->totalPages) {
            $downPage = "<a id='big' href='javascript:" . $this->ajax_func . "(" . $downRow . ")'>" . $this->config['next'] . "</a>";
        } else {
            $downPage = "";
        }
        // << < > >>
        if ($nowCoolPage == 1) {
            $theFirst = "";
            $prePage = "";
        } else {
            $preRow = $this->nowPage - $this->rollPage;
            $prePage = "<a id='big' href='javascript:" . $this->ajax_func . "(" . $preRow . ")'>上" . $this->rollPage . "页</a>";
            $theFirst = "<a id='big' href='javascript:" . $this->ajax_func . "(1)' >" . $this->config['first'] . "</a>";
        }
        if ($nowCoolPage == $this->coolPages) {
            $nextPage = "";
            $theEnd = "";
        } else {
            $nextRow = $this->nowPage + $this->rollPage;
            $theEndRow = $this->totalPages;
            $nextPage = "<a id='big' href='javascript:" . $this->ajax_func . "(" . $nextRow . ")' >下" . $this->rollPage . "页</a>";
            $theEnd = "<a id='big' href='javascript:" . $this->ajax_func . "(" . $theEndRow . ")' >" . $this->config['last'] . "</a>";
        }
        // 1 2 3 4 5
        $linkPage = "";
        for ($i = 1; $i <= $this->rollPage; $i++) {
            $page = ($nowCoolPage - 1) * $this->rollPage + $i;
            if ($page != $this->nowPage) {
                if ($page <= $this->totalPages) {
                    $linkPage .= "&nbsp;<a id='big' href='javascript:" . $this->ajax_func . "(" . $page . ")'>&nbsp;" . $page . "&nbsp;</a>";
                } else {
                    break;
                }
            } else {
                if ($this->totalPages != 1) {
                    $linkPage .= "&nbsp;<span class='current'>" . $page . "</span>";
                }
            }
        }
        $pageStr = str_replace(
            array('%header%', '%nowPage%', '%totalRow%', '%totalPage%', '%upPage%', '%downPage%', '%first%', '%prePage%', '%linkPage%', '%nextPage%', '%end%'),
            array($this->config['header'], $this->nowPage, $this->totalRows, $this->totalPages, $upPage, $downPage, $theFirst, $prePage, $linkPage, $nextPage, $theEnd), $this->config['theme']);
        return $pageStr;
    }

}
/*
 * 使用示例
 *
 *PHP 代码
        $p = new AuctionCore\Common\AjaxPage($total, $limitRows,"get_list"); //第三个参数是需要调用页面的ajax函数名
        $p->setConfig('first','首页');
        $p->setConfig('last','末页');
        $p->setConfig('theme','  %first%   %upPage%  %linkPage% %prePage% %downPage% %nextPage% %end% ');
        $limit_value = $p->firstRow . "," . $p->listRows;
        $list = M("car_series")->where("brand_id='".$brand_id."'")->select()
        $page = $p->show(); // 产生分页信息，AJAX的连接在此处生成
        $this->assign("list",$list);
        $this->assign('page',$page);

        if(I('get.ajax')){
            echo json_encode(array('list'=>$list,'page'=>$page),true);
            exit;
        }


模板代码：
            <ul id="list_ul">
              <div class="clear"></div>
            </ul>

            <div class="pageFaye" style="width:900px;padding-top:90px;margin:0px 0 0 220px;">

js代码：
<script>
    function get_list(page,url){
        var p = page;
        var url = typeof(url)=='undefined' ? '' :url;
        console.log('/HisPrice/index/ajax/1'+url)

        var new_li = '';
        $.getJSON('/HisPrice/index/ajax/1'+url, {'p':p}, function(data){
            $.each(data.list, function(index, array) { //遍历返回json
                new_li += '<li class="clearfix">';
                new_li += '<em class="tiaoShi">'+array['trade_time']+'</em>';
                new_li += '<em>'+array['reg_city']+'</em>';
                new_li += '<em class="tiaoDiqu"><a href="/Car/report/car_no/'+array['car_no']+'" target="_blank">'+array['model']+'</a></em>';
                new_li += '<em>'+array['car_color']+'</em>';
                new_li += '<em class="tiaoQian">'+array['success_price']+'</em>';
                new_li += '<em class="tiaoShi">'+array['first_reg_date']+'</em>';
                new_li += '<em>'+array['gear_type']+'</em>';
                new_li += '<em class="tiaoYong">'+array['use_properties']+'</em>';
                new_li += '</li>';
            });

            $("#list_ul").replaceWith('<ul id="list_ul">'+new_li+'<div class="clear"></div></ul>');
            $(".pager").replaceWith('<div class="pager">'+data.page+'<div class="clear"></div></div>');
        });
    }

</script>


 * */
?>